<?php

namespace app\controller;

use app\model\Salesman;
use think\captcha\facade\Captcha;
use \notification\Sms;
use app\model\Setting as SettingModel;
use think\facade\Cache;

class System
{
    public function index()
    {
        return json(['code' => 20000, 'data' => [
            'settings' => SettingModel::select()
        ]]);
    }

    public function getSmsCode()
    {
        $sms = new Sms();

        $code = rand(1000, 9999);

        $salesman = Salesman::where('phone', input('phone'))->findOrEmpty();
        if ($salesman->isEmpty()) {
            return json(['code' => 40000, 'msg' => '手机号不存在']);
        }

        $result = $sms = $sms->send(input('phone'), $code);

        Cache::set('smsCode', $code, 3600);

        if (!$result) {
            return json(['code' => 40000, 'msg' => '发送短信失败']);
        }

        return json(['code' => 20000, 'msg' => '发送短信成功']);
    }

    public function captcha()
    {
        return json(['code' => 20000, 'data' => Captcha::create()]);
    }
}
